package com.kgc.scd.service.impl;

import com.kgc.scd.config.UserCollectConfig;
import com.kgc.scd.service.SignInServerService;
import com.kgc.scd.service.SignInWebService;
import com.kgc.scd.util.JudgingTimeUtil;
import com.kgc.scd.util.RedisUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created On : 2022/11/16.
 * <p>
 * Author : yantao
 * <p>
 * Description: SignInWebServiceImpl
 */
@Slf4j
@Service
public class SignInWebServiceImpl implements SignInWebService {
    @Autowired
    private SignInServerService signInServerService;

    @Autowired
    private JudgingTimeUtil judgingTimeUtil;

    @Autowired
    private UserCollectConfig userCollectConfig;

    @Autowired
    private RedisUtils redisUtils;


    @Override
    @Async
    public void proceedSignIn(String userId,String thisday) {

        //进行格式转换,从redis中的数据
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date thisdate = null;
        try {
            //"2021-01-01 00:00:00"
            thisdate = df.parse(thisday);
        } catch (ParseException e) {
            e.printStackTrace( );
        }

        Calendar thiscal = Calendar.getInstance();
        thiscal.setTime(thisdate);
        long thistimestamp = thiscal.getTimeInMillis();



        Date date1 = new Date();
        //新用户第一次签到
        if (ObjectUtils.isEmpty(redisUtils.get(userCollectConfig.getUserLastCheckInTimePrefix()+":"+userId))){

            redisUtils.set(userCollectConfig.getUserSeriesCheckInDaysPrefix()+":"+userId,0);
            redisUtils.incr(userCollectConfig.getUserSeriesCheckInDaysPrefix()+":"+userId,1);
            //添加小积分
            signInServerService.addSmallPointsToUsersServer(userId);

            redisUtils.set(userCollectConfig.getUserLastCheckInTimePrefix()+":"+userId,thistimestamp);
            redisUtils.set(userCollectConfig.getUserThisCheckInTimePrefix()+":"+userId,thistimestamp);

            //if (!ObjectUtils.isEmpty(redisUtils.get(userCollectConfig.getUserLastCheckInTimePrefix()+":"+userId)))
        } else {
            //用户已经签到过
            redisUtils.set(userCollectConfig.getUserThisCheckInTimePrefix()+":"+userId,thistimestamp);

            date1.setTime(Long.parseLong(redisUtils.get(userCollectConfig.getUserLastCheckInTimePrefix()+":"+userId).toString()));

            //判断是否是连续签到的,上一次和这一次的天数是否大于1,大于1,不是连续签到
            if (judgingTimeUtil.differentDays(date1,thisdate)>1){
                redisUtils.set(userCollectConfig.getUserSeriesCheckInDaysPrefix()+":"+userId,0);
                redisUtils.incr(userCollectConfig.getUserSeriesCheckInDaysPrefix()+":"+userId,1);
                //TODO 增加积分
                signInServerService.addSmallPointsToUsersServer(userId);
                redisUtils.set(userCollectConfig.getUserLastCheckInTimePrefix()+":"+userId,thistimestamp);
            }else {
                //连续签到的情况
                redisUtils.incr(userCollectConfig.getUserSeriesCheckInDaysPrefix()+":"+userId,1);
                //TODO 增加积分
                signInServerService.addSmallPointsToUsersServer(userId);
                redisUtils.set(userCollectConfig.getUserLastCheckInTimePrefix()+":"+userId,thistimestamp);
            }


        }


        if (Integer.parseInt(redisUtils.get(userCollectConfig.getUserSeriesCheckInDaysPrefix()+":"+userId).toString())>=7){
            //TODO 增加大积分
            signInServerService.addLargePointsToUsersServer(userId);
            redisUtils.set(userCollectConfig.getUserSeriesCheckInDaysPrefix()+":"+userId,0);
        }

    }

    @Override
    public void updateTimeRedis(String lastday) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date lastdate = null;

        try {
            //"2021-01-01 00:00:00"
            lastdate = df.parse(lastday);


        } catch (ParseException e) {
            e.printStackTrace( );
        }
        Calendar lastcal = Calendar.getInstance();
        lastcal.setTime(lastdate);
        long lasttimestamp = lastcal.getTimeInMillis();
        redisUtils.set(userCollectConfig.getUserLastCheckInTimePrefix()+":"+"1",lasttimestamp);



    }

    @Override
    public int examineDaysRedis(String userId) {

        return Integer.parseInt(redisUtils.get(userCollectConfig.getUserSeriesCheckInDaysPrefix()+":"+userId).toString());
    }
}
